package fr.utbm.gi.vi51.project.formations;

import java.util.ArrayList;
import java.util.Collection;

import javax.vecmath.Point2d;

public class DeltaFormation extends FormationBody {

	private static final int INTER_SLOT_SPACE = 7;
	// these factors should be calculated depending on the number of units, then applied
	// private static final double DISTANCE_FACTOR = 5;
	// private static final double ANGLE_FACTOR = 0.7;
	private static final double FORMATION_ANGLE = 3.14 / 6;

	public DeltaFormation(FormationOrientation orientation) {
		super();
		this.orientation = orientation;
	}

	@Override
	public Collection<FormationSlot> scaleAt(int numberOfSlots, Point2d leaderPosition) {
		ArrayList<FormationSlot> formationSlots = new ArrayList<>();
		if (numberOfSlots > 0) {
			FormationSlot leaderSlot = new FormationSlot(null, this, 0, leaderPosition.getX(), leaderPosition.getY(), 0);
			formationSlots.add(leaderSlot);
			int a;
			int b;
			int aa;
			int bb;
			int cc;
			int dd;
			double c;
			switch (this.orientation) {
			case SOUTH:a=0;b=1;aa=1;bb=-1;cc=-1;dd=1;c=1/Math.tan(FORMATION_ANGLE);break;
			case NORTH:a=0;b=-1;aa=1;bb=1;cc=-1;dd=-1;c= 1/Math.tan(FORMATION_ANGLE);break;
			case EAST:a=1;b=0;aa=1;bb=1;cc=1;dd=1;c= Math.tan(FORMATION_ANGLE);break;
			default:
				a=-1;b=0;aa=-1;bb=1;cc=-1;dd=1;c= 1*Math.tan(FORMATION_ANGLE);
			
			}
			int nb_marine_perlanes = (numberOfSlots % 2 == 0 ? numberOfSlots / 2 : numberOfSlots / 2 + 1);
			FormationSlot firstSlot = new FormationSlot(leaderSlot, this, 1, a*INTER_SLOT_SPACE * nb_marine_perlanes,  b*INTER_SLOT_SPACE * nb_marine_perlanes,
					0);
			formationSlots.add(firstSlot);

			for (int index = 1; index < numberOfSlots / 2; index++) {
				FormationSlot laneSlot = new FormationSlot(firstSlot, this, index + 1, -aa*INTER_SLOT_SPACE * index
						/ c, bb*INTER_SLOT_SPACE * index * c, 0);
				formationSlots.add(laneSlot);
			}

			for (int index = numberOfSlots / 2 + 1; index < numberOfSlots; index++) {
				FormationSlot laneSlot = new FormationSlot(firstSlot, this, index, -cc*INTER_SLOT_SPACE
						* (index - numberOfSlots / 2) /c, -dd*INTER_SLOT_SPACE
						* (index - numberOfSlots / 2) * c, 0);
				formationSlots.add(laneSlot);
			}
		}
		return formationSlots;
	}

}
